using huqiang.Core.HGUI;
using System;
using UnityEngine;
using huqiang.UIComposite;
using huqiang.UIEvent;
using huqiang;
using huqiang.Data;
using huqiang.UIModel;
using System.Collections.Generic;

public class ScrollPage:UIPage
{
    //反射UI界面上的物体,请确保名称唯一，且于字段声明相同
    class View
    {
        public GridScroll gridScroll;
        public ScrollY scrollY;
        public ScrollX scrollX;
    }
    class Item
    {
        public UserEvent Image;
        public HText Text;
        public int index;
    }
    View view;
    public override void Initial(UIElement parent, object dat = null)
    {
        base.Initial(parent, dat);
        view = LoadUI<View>("baseUI", "ScrollPage");//"baseUI"创建的bytes文件名,"page"为创建的页面名
        InitialData();
        InitialEvent();
    }
    void InitialData()
    {
        view.gridScroll.DataLength = 100;
        view.gridScroll.Column = 10;
        List<string> dat = new List<string>();
        for (int i = 100; i < 200; i++)
            dat.Add(i.ToString());
        view.scrollY.BindingData = dat;
        view.scrollX.BindingData = dat;
    }
    void InitialEvent()
    {
        view.gridScroll.SetItemUpdate<Item, int>(GridItemUpdate);
        view.gridScroll.Refresh();
        view.scrollY.SetItemUpdate<Item, string>(StringItemUpdate);
        view.scrollX.SetItemUpdate<Item, string>(StringItemUpdate);
        //热更新使用下面的方法，它使用了一个类型转换的中间件，可用于热更新的泛型
        //var middle = new HotConstructor<Item, string>(StringItemUpdate).middle;
        //view.scrolly.SetItemUpdate(middle);
        //view.scrollx.SetItemUpdate(middle);
        view.scrollY.Refresh();
        view.scrollX.Refresh();
    }
    void GridItemUpdate(Item item,int dat,int index)
    {
        item.index = index;
        item.Image.DataContext = item;
        item.Text.Text = index.ToString();
        item.Image.Click = (o, e) => {
            var i = o.DataContext as Item;
            Debug.Log("项目单击索引："+i.index);
        };
    }
    void StringItemUpdate(Item item,string dat,int index)
    {
        item.index = index;
        item.Image.DataContext = item;
        item.Text.Text = dat;
        item.Image.Click = (o, e) => {
            var i = o.DataContext as Item;
            Debug.Log("项目单击索引：" + i.index);
        };
    }
}
